<?php include("../../data/mysql.class.php")?>
<?php include("../../data/Function.php")?>
<?
$i=0;
if(isset($_GET["Type"])){$Type=$_GET["Type"];}
if(isset($_POST["Type"])){$Type=$_POST["Type"];}
$conn=new Mysql();
switch($Type){
    case "checkall":
        $sql = "select id,CatalogParentId,title from newscata_product  order by SortId asc";
        $row=$conn->getAll($sql);
        $arry[]=array();
        foreach($row as $k=>$rss){
            $arry[$i]["id"]=$rss["id"];
            $arry[$i]["pId"]=$rss["CatalogParentId"];
            $arry[$i]["name"]=$rss["title"];
            //if($i==0){$arry[$i]["open"]=true;}
            $arry[$i]["open"]=true;
            $i++;
        }
        echo json_encode($arry);
        break;
    case "add":
        $cat_add_name = trim($_POST["title"]);
        $title_f = trim($_POST["title_f"]);
        $doid=trim($_POST["sel_Sub"]);
        $ainipic = trim($_POST["tpic"]);
        $xiaji = trim($_POST["xiaji"]);
        $AInfoType = trim($_POST["InfoType"]);
        $logo = trim($_POST["logo"]);
        $banner = trim($_POST["banner"]);
        $banner2 = trim($_POST["banner_wap"]);
        $JumpUrl=$_POST["JumpUrl"];
        $articletitle=$_POST["ttitle"];
        $articlekeyword=$_POST["tkeyword"];
        $articledescription=$_POST["tdescription"];
        $recc = trim($_POST["recc"]);
        $dirpath = trim($_POST["dirpath"]);
        $rsp_query ="select * from newscata_product where id=".$doid;
        $rsp =$conn->getRow($rsp_query);
        if($rsp){
            $arootid = $rsp["rootid"];//==根ID号==
            $alevel = $rsp["Mlevel"];//==树深度==
        }
        else{
            $arootid = "";	//==根ID号==
            $alevel = 0;	//==树深度==
        }
        $alevel++;

        if($arootid ==""){
            $rsarootid = "*";
            $rsaMyrootid = ",*,";
        }
        else{
            $rsarootid = $arootid.",*";
            $rsaMyrootid = ",".$arootid.",*,";
        }
        //mysql_free_result($rsp_query);
        $ins_sql = "INSERT INTO `newscata_product` (`title`,`title_f`, `CatalogParentId`, `Mlevel`, `SortId`, `rootid`, `MyRootid`, `tp`, `atime`, `leiid`, `xia`, `InfoType`, `logo`, `banner`,`banner_wap`, `JumpUrl`, `recc`, `articletitle`, `articlekeyword`, `articledescription`, `dirpath`) VALUES ('$cat_add_name','$title_f', '$doid', '$alevel', '1', '$rsarootid', '$rsaMyrootid', '$ainipic', NOW(), NULL, $xiaji, '$AInfoType', '$logo', '$banner', '$banner2', '$JumpUrl', $recc, '$articletitle', '$articlekeyword', '$articledescription','$dirpath')";
        $menuadd = $conn->query($ins_sql);  //执行

        if($menuadd)
        {
            //echo "插入成功!";
        }
        else
        {
            //echo $ins_sql;

        }
        $gid = $conn->lastId(); //获取插入的ID号

        $showquery ="select * from newscata_product order by id desc LIMIT 1";
        $rsU =$conn->getRow($showquery);
        if($rsU)
        {
            $gidu = $rsU["id"];
            $groot = $rsU["rootid"];
            $c_groot = str_replace("*",$gidu,$groot);
            $rsUMyrootid = str_replace("*",$gidu,$rsU["MyRootid"]);
        }

        $menuupdate = $conn->query("update newscata_product set `rootid`='$c_groot',`MyRootid`='$rsUMyrootid',`SortId`='$gid' where `id`='$gid'");
        if($menuupdate)
        {
            echo 1;
        }
        else
        {
            echo 0;
            //echo "update newscata_product set `rootid`='$c_groot',`MyRootid`='$rsUMyrootid',`SortId`='$gid' where `id`='$gid'";

        }
        break;
    case "delete":
        $TypeId=$_POST["id"];
        $conn->query("delete from news_shop where type in ($TypeId)");
        $conn->query("delete from newscata_product where id in ($TypeId)");
        echo 1;
        break;
    case "edit":
        $ids = trim($_POST["uid"]);
        $cat_add_name = trim($_POST["title"]);
        $title_f = trim($_POST["title_f"]);
        $doid=trim($_POST["sel_Sub"]);
        $ainipic = trim($_POST["tpic"]);
        $xiaji = trim($_POST["xiaji"]);
        $AInfoType = trim($_POST["InfoType"]);
        $logo = trim($_POST["logo"]);
        $banner = trim($_POST["banner"]);
        $banner2 = trim($_POST["banner_wap"]);
        $JumpUrl=$_POST["JumpUrl"];
        $articletitle=$_POST["ttitle"];
        $articlekeyword=$_POST["tkeyword"];
        $articledescription=$_POST["tdescription"];
        $recc = trim($_POST["recc"]);
        $dirpath = trim($_POST["dirpath"]);
        $SortId = trim($_POST["SortId"]);

        $sql= "update `newscata_product` set JumpUrl='$JumpUrl',title='$cat_add_name',title_f='$title_f',tp='$ainipic',xia=$xiaji,banner='$banner',InfoType='$AInfoType',`articletitle`='$articletitle',`articlekeyword`='$articlekeyword',`articledescription`='$articledescription',logo='$logo',banner='$banner',banner_wap='$banner2',dirpath='$dirpath',SortId=$SortId where id= '$ids'";
        if($conn->query($sql)){
            echo 1;
        }else{
            echo $sql;
        }
        break;
    default:
        break;
}


?>